﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace DMSWcfServices.xmls
{
    public partial class getXmlOrderInvoice_AUD : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Xml que retorna los eventos de la pagina
            Response.Expires = 0;
            Response.ExpiresAbsolute = DateTime.MinValue;
            Response.AddHeader("PRAGMA", "NO-CACHE");
            Response.AddHeader("Expires", "-1");
            Response.CacheControl = "no-cache";
            Response.ContentType = "text/xml";

            WSHttpBinding binding = new WSHttpBinding();
            binding.CloseTimeout = new TimeSpan(0, 1, 0);
            binding.OpenTimeout = new TimeSpan(0, 1, 0);
            binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
            binding.SendTimeout = new TimeSpan(0, 2, 0);
            binding.Security.Mode = SecurityMode.None;

            CustomBinding customBinding = new CustomBinding();

            EndpointAddress address = new EndpointAddress(Request.QueryString["PSDS"].ToString());

            DMSServices.DMSServicesClient service = new DMSServices.DMSServicesClient(binding, address);

            List<Dtl.OrderInvoice_AUD> listOrderInvoice_AUD = null;
            try
            {
                //+ "&PSDS=" + Properties.Settings.Default.Server
                string cdDistributor = Request.QueryString["cdDistributor"].ToString();
                string opcionReport = Request.QueryString["opcionReport"].ToString();//1:ORDERS : 2:INVOICES
                DateTime dtInvoiceStart = Convert.ToDateTime(Request.QueryString["dtStart"].ToString());
                DateTime dtInvoiceEnd = Convert.ToDateTime(Request.QueryString["dtEnd"].ToString());
                listOrderInvoice_AUD = service.getOrderInvoice_AUD_Report(cdDistributor, opcionReport, dtInvoiceStart, dtInvoiceEnd, Request.QueryString["ambient"].ToString());
            }
            catch (Exception ee)
            {
                listOrderInvoice_AUD = new List<Dtl.OrderInvoice_AUD>();
            }

            XmlTextWriter cdRSS = new XmlTextWriter(this.Response.OutputStream, System.Text.Encoding.UTF8);
            cdRSS.WriteStartDocument();
            cdRSS.WriteStartElement("OrderInvoice_AUD");

            if (listOrderInvoice_AUD.Count > 0)
            {
                foreach (Dtl.OrderInvoice_AUD oi in listOrderInvoice_AUD)
                {
                    cdRSS.WriteStartElement("Items_AUD");
                    cdRSS.WriteAttributeString("distributor", oi.distributor);
                    cdRSS.WriteAttributeString("errors", oi.errors);
                    cdRSS.WriteAttributeString("display", oi.display);
                    cdRSS.WriteAttributeString("iddoc", oi.iddoc);
                    cdRSS.WriteAttributeString("docfecha", oi.docFecha.ToString());
                    cdRSS.WriteAttributeString("idproduct", oi.idProduct);
                    cdRSS.WriteAttributeString("idstore", oi.idStore);
                    cdRSS.WriteAttributeString("idregion", oi.idRegion);
                    cdRSS.WriteAttributeString("nrQuantity", oi.nrQuantity);
                    cdRSS.WriteAttributeString("vlDiscount", oi.vlDiscount);
                    cdRSS.WriteAttributeString("vltotal", oi.vlTotal);
                    cdRSS.WriteAttributeString("estatus", oi.estatus);

                    cdRSS.WriteEndElement();
                }
            }


            cdRSS.WriteEndDocument();
            cdRSS.Flush();
            cdRSS.Close();
            this.Response.End();
        }
    }
}
